﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace InternetSpiderLib
{
    /// <summary>
    /// 场景管理器
    /// </summary>
    public class SceneManager
    {
        /*  场景管理器
         *      管理所有场景的容器，可以对场景增删改查
         */

        private List<SceneItem> _SceneItems = new List<SceneItem>();
        private ReaderWriterLock _RWLock = new ReaderWriterLock();

        /// <summary>
        ///  触发场景
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public int TriggerScene(DataBox data)
        {
            int count = 0;
            _RWLock.Reader(() =>
            {
                DataBox temp = new DataBox(data);
                _SceneItems.ForEach(n => count += n.Run(temp) ? 1 : 0);
            });
            return count;
        }

        /// <summary>
        /// 注册场景
        /// </summary>
        /// <param name="scene"></param>
        public void RegisterScene(SceneItem scene)
        {
            _RWLock.Writer(() =>
            {
                _SceneItems.Add(scene);
            });
        }
    }
}
